﻿using SheepStorage.Domain.Entity;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SheepStorage.Service.DataImp
{
    /// <summary>
    /// 角色服务层
    /// </summary>
    public class RolesService: IRolesService
    {
        private readonly IBaseRepository<Role> Rolerepository;

        //角色菜单关系表
        IBaseRepository<RoleMenu> RoleMenurepository;

        public RolesService(IBaseRepository<Role> Rolerepository, IBaseRepository<RoleMenu> RoleMenurepository)
        {
            this.Rolerepository = Rolerepository;
            this.RoleMenurepository = RoleMenurepository;
		}

       /// <summary>
       /// 查询所有角色
       /// </summary>
       /// <param name="roleName"></param>
       /// <returns></returns>
        public async Task< List<Role>> GetAllRoles( string ?roleName)
        {
            var roleList = Rolerepository.GetAll().Where(c=>c.IsDel==0).ToList();

            if (!string.IsNullOrEmpty(roleName))
            {
                roleList = roleList.Where(c => c.RoleName.Contains(roleName)).ToList();
            }




            return roleList;
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        public async Task<int> AddRole(Role role)
        {
            return Rolerepository.Add(role);
        }

        /// <summary>
        /// 根据ID查看角色详细信息
        /// </summary>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public async Task<Role> GetAllRolesById(int RoleId)
        {
            var roleentity = Rolerepository.GetAll().FirstOrDefault(c => c.RoleId == RoleId &&  c.IsDel == 0);
            return roleentity;
        }
        //逻辑删除角色信息
        public async Task<int> DeleteRole(int RoleId)
        {
            var roleentity = Rolerepository.GetAll().FirstOrDefault(c => c.RoleId == RoleId);
            roleentity.IsDel = 1;
            return Rolerepository.Update(roleentity);
        }
        //修改角色信息
        public async Task<int> UpdateRole(Role role)
        {
            var res= Rolerepository.Update(role);
            return res;
        }
       //根据角色分配菜单权限
        public async Task< int> AddMenuToRole(int RoleId, List<int> menuIds)
        {
            //删除原有菜单权限 
            var roleMenuList = RoleMenurepository.GetAll().Where(c => c.RoleId == RoleId).ToList();
            if (roleMenuList.Count > 0)
            {
                RoleMenurepository.DeleteRange(roleMenuList);
            }
            //添加新菜单权限
            if (menuIds.Count > 0)
            {
				foreach (var item1 in menuIds)
				{
					var menupermiss = new RoleMenu()
					{
						 RoleId = RoleId,
                         MenuId = item1,

					};
					var res=   RoleMenurepository.Add(menupermiss);
				}
			}
            return  1;

        }
    }
}
